using System.Collections.Generic;
using UnityEngine;

public class TankArrows : MonoBehaviour
{
	public List<Transform> arrows;

	public BezierCurve bezierCurve;

	private RCC_CarControllerV3 carController;

	private float sign = 1f;

	private bool initialized;

	private float arrowStep = 1f;

	private Vector3 _initialBezierPoints = Vector3.zero;

	private Vector3 _initialHandle = Vector3.zero;

	public float initialBezierPointXmult = 2f;

	public float currentBezierPointX;

	private List<Vector3> newPoints;

	private Vector3 initialBezierPoints
	{
		get
		{
			if (_initialBezierPoints == Vector3.zero)
			{
				_initialBezierPoints = bezierCurve.BezierPoints[1].transform.localPosition;
			}
			return _initialBezierPoints;
		}
		set
		{
		}
	}

	private Vector3 initialHandle
	{
		get
		{
			if (_initialHandle == Vector3.zero)
			{
				_initialHandle = bezierCurve.BezierPoints[1].handle1;
			}
			return _initialHandle;
		}
		set
		{
		}
	}

	public void Init(RCC_CarControllerV3 carController, bool frontArrows)
	{
		this.carController = carController;
		initialBezierPoints = initialBezierPoints;
		initialHandle = initialHandle;
		arrowStep = 1f / (float)arrows.Count;
		if (frontArrows)
		{
			sign = 1f;
		}
		else
		{
			sign = -1f;
		}
		initialized = true;
	}

	private void Update()
	{
		if (initialized)
		{
			SetArrowCurve();
			SetArrowsPosition();
		}
	}

	private void SetArrowCurve()
	{
		currentBezierPointX = bezierCurve.BezierPoints[1].transform.localPosition.x / (initialBezierPointXmult * initialBezierPoints.x);
		bezierCurve.BezierPoints[1].transform.localPosition = new Vector3(sign * Mathf.Abs(initialBezierPoints.x) * initialBezierPointXmult * carController.steerInput, initialBezierPoints.y, initialBezierPoints.z * (2f - Mathf.Abs(currentBezierPointX)));
		float num = 0f;
		num = (0.5f - Mathf.Abs(currentBezierPointX)) * 2f;
		num = Mathf.Clamp(num, -1f, 1f);
		if (sign > 0f)
		{
			bezierCurve.BezierPoints[1].handle1 = new Vector3((0f - Mathf.Abs(initialHandle.x)) * sign * currentBezierPointX, initialHandle.y, 0f - num);
		}
		else
		{
			bezierCurve.BezierPoints[1].handle2 = new Vector3((0f - Mathf.Abs(initialHandle.x)) * sign * currentBezierPointX, initialHandle.y, num);
		}
	}

	private void SetArrowsPosition()
	{
		newPoints = new List<Vector3>();
		newPoints.Clear();
		for (float num = 0f; num <= 1f; num += arrowStep)
		{
			newPoints.Add(bezierCurve.GetPointAt(num));
		}
		for (int i = 0; i < arrows.Count; i++)
		{
			arrows[i].position = newPoints[i];
			arrows[i].forward = newPoints[i + 1] - newPoints[i];
		}
	}
}
